package cn.xpg.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Customer;
import cn.xpg.service.CustomerService;
import cn.xpg.tools.PageSupport;

@Controller
public class CustomerController {

	@Resource(name = "customerService")
	private CustomerService customerService;

	// 分页查询客户
	@RequestMapping("/getCustomerList.do")
	public String getCustomerList(HttpServletRequest request) {
		String pageIndex = request.getParameter("pageIndex");
		String customer_name = request.getParameter("customer_name");
		String jname = request.getParameter("jname");
		int pageSize = 3;
		int currentPageNo = 1;

		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		int totalCount = 0;
		totalCount = customerService.getCustomerCount(customer_name);
		System.out.println("查询客户信息！！！！！！！！！！————>getCustomerList");
		System.out.println(jname);
		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Customer> customerList = null;
		customerList = customerService.getCustomerList(customer_name, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("customerList", customerList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("customer_name", customer_name);
		if (jname.equals("base")) {
			return "/jsp/base/customer_list";
		} else {
			return "/jsp/system/manage_kh";
		}
	}

	// 新增客户
	@RequestMapping("addCustomer.do")
	public String addCustomer(@Valid Customer customer) {
		System.out.println("新增客户信息！！！！！！！！！！————>addCustomer");
		if (customerService.addCustomer(customer) > 0) {
			return "redirect:/getCustomerList.do?jname=base";
		} else {
			return "/jsp/base/customer_list";
		}
	}

	// 获取客户Id
	@RequestMapping("getCustomerId.do")
	public String getCustomerId(HttpServletRequest request) {
		System.out.println("获取客户Id！！！！！！！！！！————>getCustomerId");
		int customer_id = Integer.parseInt(request.getParameter("customer_id"));
		Customer customer = customerService.getCustomerId(customer_id);
		request.setAttribute("customer", customer);
		return "/jsp/base/customer_update";
	}

	// 修改客户
	@RequestMapping("updateCustomer.do")
	public String updateCustomer(@Valid Customer customer, HttpServletRequest request) {
		System.out.println("修改客户信息！！！！！！！！！！————>updateCustomer");
		if (customerService.updateCustomer(customer) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getCustomerList.do?jname=base";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("customer", customer);
			return "/jsp/base/customer_update";
		}
	}

	// 删除客户
	@RequestMapping("deleteCustomer.do")
	public String deleteCustomer(HttpServletRequest request) {
		System.out.println("删除客户信息！！！！！！！！！！————>deleteCustomer");
		int customer_id = Integer.valueOf(request.getParameter("customer_id"));
		if (customerService.deleteCustomer(customer_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getCustomerList.do?jname=base";
	}
}
